/**
 * 
 */
package com.dcivision.dms.core;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

import com.dcivision.framework.AutoCompleteInputBoxManager;
import com.dcivision.framework.DataSourceFactory;
import com.dcivision.framework.SessionContainer;

/**
AutoSuggestIndexValueManager.java
a manager get index value for auto suggest.
@author      Jim Zhou
@company     DCIVision Limited
@creation date   2006/09/19
@version     $Revision: 1.4.2.1 $
*/
public class AutoSuggestIndexValueManager implements AutoCompleteInputBoxManager {
  
  protected Log log = new LogFactoryImpl().getInstance(this.getClass());
  public static final String UDFD_ID = "UDFD_ID";
  public static final String FIELD_ID = "FIELD_ID";
  
  public AutoSuggestIndexValueManager() {
    super();
    
  }
  
  /** (non-Javadoc)
   * @see com.dcivision.framework.AutoCompleteInputBoxManager#getOnReceiveJSFunction()
   */
  public String getOnReceiveJSFunction() {
    
    return null;
  }
  
  /** (non-Javadoc)
   * @see com.dcivision.framework.AutoCompleteInputBoxManager#getOnSelectJSFunction()
   */
  public String getOnSelectJSFunction() {
    
    return null;
  }
  
  /** (non-Javadoc)
   * @see com.dcivision.framework.AutoCompleteInputBoxManager#getOnTimeoutJSFunction()
   */
  public String getOnTimeoutJSFunction() {
    
    return null;
  }
  
  /**
   * get string list for dwr function .
   * list data will get from system default index value or user defined document profile index value.
   * @param   args                                String[]
   * @param   sessionContainer                    SessionContainer
   * @return                                      datalist for auto suggest.
   */  
  public List retrieveInformation(String[] args,
      SessionContainer sessionContainer) {
    
    Connection conn = null;
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    ArrayList result = new ArrayList();
    
    //get data list
    if (args.length>0){
      String[] para1 = args[0].split(":"); 
      try{
        //get connection.
        conn = DataSourceFactory.getConnection();
        synchronized (conn) {
            if (para1[0].equals(this.UDFD_ID)){
              //system default index value
              //sqlStat.append("SELECT DISTINCT F.FIELD_VALUE FROM ( SELECT DISTINCT FIELD_VALUE, UPDATE_DATE FROM DMS_DOCUMENT_DETAIL WHERE (CREATOR_ID = '" + sessionContainer.getUserRecordID() + "' OR UPDATER_ID = '" + sessionContainer.getUserRecordID() + "') AND FIELD_VALUE <> '' AND USER_DEFINED_FIELD_DETAIL_ID = '" + para1[1] + "'  ORDER BY UPDATE_DATE DESC ) F " );//Update By Jim Zhou      2007/01/08    EIP-1326
              sqlStat.append("SELECT FIELD_VALUE,MAX(UPDATE_DATE) UPDATE_DATE FROM   DMS_DOCUMENT_DETAIL WHERE  (CREATOR_ID = '" + sessionContainer.getUserRecordID() + "'         OR UPDATER_ID = '" + sessionContainer.getUserRecordID() + "')         AND FIELD_VALUE <> ''         AND USER_DEFINED_FIELD_DETAIL_ID = '" + para1[1] + "'  GROUP BY FIELD_VALUE ORDER BY UPDATE_DATE DESC");
              
              preStat = conn.prepareStatement(sqlStat.toString());
              rs = preStat.executeQuery();
              while(rs.next()){
                result.add(rs.getString("FIELD_VALUE"));
              }
            } else if (para1[0].equals(this.FIELD_ID)) {
              //user defined document profile index value
              //sqlStat.append("SELECT DISTINCT U.USER_DEF_" + para1[1] + " FROM ( SELECT USER_DEF_" + para1[1] + ", UPDATE_DATE FROM DMS_DOCUMENT WHERE (CREATOR_ID = '" + sessionContainer.getUserRecordID() + "' OR UPDATER_ID = '" + sessionContainer.getUserRecordID() + "') AND USER_DEF_" + para1[1] + " <> '' ORDER BY UPDATE_DATE DESC ) U");//Update By Jim Zhou      2007/01/08    EIP-1326
              sqlStat.append("SELECT USER_DEF_" + para1[1] + ",MAX(UPDATE_DATE) UPDATE_DATE FROM DMS_DOCUMENT WHERE ( CREATOR_ID = '" + sessionContainer.getUserRecordID() + "'       OR UPDATER_ID = '" + sessionContainer.getUserRecordID() + "')       AND USER_DEF_" + para1[1] + " <> ''  GROUP BY USER_DEF_" + para1[1] + " ORDER BY UPDATE_DATE DESC");
              
              preStat = conn.prepareStatement(sqlStat.toString());
              rs = preStat.executeQuery();
              while(rs.next()){
                result.add(rs.getString("USER_DEF_" + para1[1]));
              }
            }
            return result;
        }
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
      } catch (Exception e) {
        log.error(e, e);
      } finally {
        try {
          rs.close();
        } catch (Exception ignore) {} finally {
          rs = null;
        }
        try {
          conn.close();
        } catch (Exception ignore) {} finally {
          conn = null;
        }
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
      }
    }
    return null;
  }
  
}
